package com.mtiiot.fsmanage.utils;

import java.io.*;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * Created by xyr on 2016/9/1.
 * 文件操作工具类
 */
public class FileUtil {


    /**
     * 将byte数组转成file
     * */
    public static File bytesToFile(byte[] bytes,String fileName){
        BufferedOutputStream outputStream = null;
        FileOutputStream fileOutputStream = null;
        File file = null;
        try {
            file = new File(fileName);
            fileOutputStream = new FileOutputStream(file);
            outputStream = new BufferedOutputStream(fileOutputStream);
            outputStream.write(bytes);
            outputStream.flush();
        } catch (Exception e){
            outputStream = null;
            fileOutputStream = null;
        } finally {
            if (outputStream != null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileOutputStream != null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return file;
    }

    /**
     * 将文件打包成zip
     * */
    public static void zipFiles(File targetFile,List<File> fileList){
        ZipOutputStream zipOutputStream = null;
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        try {
            zipOutputStream = new ZipOutputStream(new FileOutputStream(targetFile));
            int len = fileList.size();
            byte[] bytes = new byte[1024*10];
            for (int i = 0; i < len; i++){
                File file = fileList.get(i);
                // 创建zip实体，并添加进压缩包
                ZipEntry entry = new ZipEntry(file.getName());
                zipOutputStream.putNextEntry(entry);

                // 读取待压缩的文件并写进压缩包里
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                int read = 0;
                while((read=bis.read(bytes, 0, 1024*10)) != -1){
                    zipOutputStream.write(bytes,0,read);
                }
            }
        } catch (Exception e){
            zipOutputStream = null;
            fis = null;
            bis = null;
        }finally {
            if (zipOutputStream != null){
                try {
                    zipOutputStream.close();
                } catch (IOException e) {
                    zipOutputStream = null;
                }
            }
            if (fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    fis = null;
                }
            }
            if (bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    bis = null;
                }
            }
        }

    }
}
